'use strict';
// 获取数据库里的数据
const db = uniCloud.database()
const $ = db.command.aggregate
exports.main = async (event, context) => {
	const {
		user_id,
		type
	} = event
	
	let matchObj ={}
	if(type !=='all'){
		matchObj = {
			current:true
		}
	}
	
	let userInfo = await db.collection('user').doc(user_id).get()
	userInfo = userInfo.data[0]

	let label = await db.collection('label')
	.aggregate()
	.addFields({
		                         //$.ifNull()如果没有这个字段就返还第二个参数
		current:$.in(['$_id',$.ifNull([userInfo.label_ids,[]])])
	})
	.match(matchObj)
	.end()
	
	//返回数据给客户端
	return {
		code: 200,
		msg: '数据获取成功',
		data: label.data
	}

};
